package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.HomeCateTwo;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.example.peibei.R;
import com.example.peibei.service.presenter.HomeBannerCatePresenter;
import com.example.peibei.ui.adapter.ChooseCate2Adapter;
import com.example.peibei.ui.adapter.ChooseCateAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChooseCate2Activity extends WDActivity {
    @BindView(R.id.rv_cate)
    RecyclerView rv_cate;

    private HomeBannerCatePresenter homeBannerCatePresenter;
    List<HomeCateTwo.CategoryDTO> cates = new ArrayList<>();
    private ChooseCate2Adapter chooseCateAdapter;
    private int categoryId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_cate2;
    }

    @Override
    protected void initView() {
        rv_cate.setLayoutManager(new LinearLayoutManager(this));
        chooseCateAdapter = new ChooseCate2Adapter(cates);
        rv_cate.setAdapter(chooseCateAdapter);
        homeBannerCatePresenter = new HomeBannerCatePresenter(new CateCall());
        homeBannerCatePresenter.reqeust();
        chooseCateAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                HomeCateTwo.CategoryDTO data = cates.get(position);
                if (!data.isChoosed()) {
                    data.setChoosed(true);
                    categoryId = data.getId();
                }
                for (int i = 0; i < cates.size(); i++) {
                    HomeCateTwo.CategoryDTO categoryDTO = cates.get(i);
                    if (position != i) {
                        categoryDTO.setChoosed(false);
                    }
                }
                chooseCateAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.tv_next)
    public void next() {
        Bundle bundle = new Bundle();
        bundle.putInt("categoryId", categoryId);
        intent(ChooseOrderContent2Activity.class, bundle);
        finish();
    }

    class CateCall implements DataCall<HomeCateTwo> {

        @Override
        public void success(HomeCateTwo data, Object... args) {
            cates.clear();
            cates.addAll(data.getCategory());
            cates.get(0).setChoosed(true);
            categoryId = cates.get(0).getId();
            chooseCateAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }

    }

    @Override
    protected void destoryData() {

    }
}
